package com.hdit.action.shop;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.jsp.PageContext;

import com.hdit.entity.AuctionApply;
import com.hdit.service.AuctionApplyService;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.web.ServletCacheAdministrator;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * 前台Action类 - 订单处理
 * ============================================================================
 */

@ParentPackage("member")
@InterceptorRefs({
	@InterceptorRef(value = "token", params = {"excludeMethods", "info,list,view,add"}),
	@InterceptorRef(value = "memberStack")
})
public class AuctionApplyAction extends BaseShopAction {
	
	private static final long serialVersionUID = 2553137844831167917L;
	
	
	
	
	private AuctionApply auctionApply;//商品申请

	@Resource
	private AuctionApplyService auctionApplyService;
	
	// 订单信息
	@InputConfig(resultName = "error")
	public String info() {
		
		setResponseNoCache();
		return "info";
	}
	
	// 订单保存
	@Validations(
		requiredStrings = {
			@RequiredStringValidator(fieldName = "auctionApply.auctionNo", message = "专利号必须输入！"),
			@RequiredStringValidator(fieldName = "auctionApply.name", message = "专利名称必须输入！"),
			@RequiredStringValidator(fieldName = "auctionApply.auctionPrice", message = "竞拍底价必须输入！")
		}
	)
	@InputConfig(resultName = "error")
	public String save() {
		auctionApply.setCreateDate(new Date());
		auctionApply.setStatus(0);
		auctionApply.setMember(this.getLoginMember());
		auctionApplyService.save(auctionApply);
		setResponseNoCache();
		return SUCCESS;
	}
	
	
	
	// 竞拍申请列表
	public String list() {
		
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(AuctionApply.class);
		detachedCriteria.add(Restrictions.eq("member", getLoginMember()));
		pager = auctionApplyService.findByPager(pager, detachedCriteria);
		return "list";
	}
	
	// 订单详情
	public String view() {
//		order = orderService.load(id);
//		totalPoint = 0;
//		if (getSystemConfig().getPointType() == PointType.productSet) {
//			for (OrderItem orderItem : order.getOrderItemSet()) {
//				totalPoint = orderItem.getProduct().getPoint() * orderItem.getProductQuantity() + totalPoint;
//			}
//		} else if (getSystemConfig().getPointType() == PointType.orderAmount) {
//			totalPoint = order.getProductTotalPrice().multiply(new BigDecimal(getSystemConfig().getPointScale().toString())).setScale(0, RoundingMode.DOWN).intValue();
//		}
		return "view";
	}
	
	// 添加
	public String add() {
		
		System.out.println("=======开始申请专利拍卖========");
		return INPUT;
	}
	
	// 更新页面缓存
	private void flushCache() {
		Cache cache = ServletCacheAdministrator.getInstance(getRequest().getSession().getServletContext()).getCache(getRequest(), PageContext.APPLICATION_SCOPE); 
		cache.flushAll(new Date());
	}

	public AuctionApply getAuctionApply() {
		return auctionApply;
	}

	public void setAuctionApply(AuctionApply auctionApply) {
		this.auctionApply = auctionApply;
	}


}